#define _GUN_SOURCE
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include "../utils/net.h"

int main(int argc, char* argv[]) {
    HTTP_REQUEST req={0};
    HTTP_RESPONSE res={0};
   
    // Initialize response
    strcpy(res.version,"HTTP/1.1");
    res.status_code=200;
    strcpy(res.status_msg,"Ok");
    strcpy(res.headers,"Content-Type: text/html\r\nBear: 123\r\n");
    strcpy(res.body,"<html><head><title>C HTTP Server</title></head>"
        "<body>"
        "<h1>Welcome to C HTTP Server</h1>"
        "<p>This is a simple HTTP server implemented in C</p>"
        "<p>Try these routes:</p>"
        "<ul>"
        "<li><a href=\"/time\">Current Time</a></li>"
        "<li><a href=\"/echo?message=Hello\">Echo Service</a></li>"
        "</ul>"
        "</body></html>");
    // listen for incoming connections
    int listen_socket = listen_from(argv[1], SOCK_STREAM, 10);
    if (listen_socket<0) {
        printf("Failed to listen on socket\n");
        return 1;
    }
    // accept incoming connection
    int client_socket = accept(listen_socket, NULL, NULL);
    if (client_socket<0) {
        printf("Failed to accept incoming connection\n");
        return 1;
    }

    recv_http_request_v1(client_socket,&req,NULL);

    // send HTTP response
    send_http_response_v1(client_socket,&res,NULL,0);
    // receive HTTP request
    printf("mothed: %s url: %s version: %s\nheaders:\n%s\nbody:\n%s\n",req.method,req.url,req.version,req.headers,req.body);
    return 0;
}
